package edu.ruc.cluster.action;

import java.io.IOException;

import edu.ruc.cluster.util.AbstractInteractiveCommand;
import edu.ruc.cluster.util.Const;
import edu.ruc.cluster.util.PropertyRetrieval;

/**
 * 2012-12-12
 * Used: UnTar the Const.JDK_TAR_FILE in sudoer's home folder in each machine
 * 		 Move it to Const.JDK_DST_TAG in Const.JDK_CONF_FILE
 * 		 update-alternatives --install java and javac
 * 		 chmod +x /Const.JDK_DST_TAG/Const.JDK_FOLDER_TAG/java/bin/*
 * 		 update-alternative --config java
 * @author deke
 */
public class LocateJDKCmd extends AbstractInteractiveCommand {

	private String jdkpath = null;
	public LocateJDKCmd(String host, String passwd){
		super(host, passwd);
	}
	@Override
	public void interactProcess(String cmd) {
		// TODO Auto-generated method stub
		String[] commands = cmd.split("xxx");
		try{
			int count = 1;
			char selection = '\0';
			String buffer = "";
			for(int i = 0 ; i < commands.length; i ++){
				writor.write((commands[i]+ "\n").getBytes());
				writor.flush();
							
		    	char[] cbuf = new char[1024];			
				int ret = reader.read(cbuf);
				
				while(ret != -1){
					String info = new String(cbuf, 0, ret);
					System.out.print(info);
					
					if(i == commands.length - 1 && selection == '\0'){
						buffer += info;
						if(buffer.contains(jdkpath)){
							for(int j = info.indexOf(jdkpath); j >= 0 ; j --){
								char ch = info.charAt(j);
								if(ch >= '1' && ch <= '9'){
									selection = ch;
									break;
								}
							}
						}
					}
					if(info.contains("[" + getUser()+ "@" )){	
//					if(info.contains("@" + getHost())){
						if(count == 0){
							break;
						}
						count --;
					}else if(info.contains("password")){
						writor.write((getPasswd() + "\n").getBytes());
						writor.flush();
					}else if(info.contains("selection number")){
						if(selection != '\0'){
							writor.write((selection + "\n").getBytes());
							writor.flush();
						}else{
							logHandler.error("Cannot get java selection number !");
							break;
						}
					}
					ret = reader.read(cbuf);
				}
			}
			
		}catch(IOException e){
			logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
		}
		
	}
	public void confjdk(){
		PropertyRetrieval pr = new PropertyRetrieval(Const.CONFIG_FILE);
		String cmd1 = "tar -zxf " + Const.JDK_TAR_FILE;
		String cmd2 = "sudo cp -r " + pr.getString(Const.JDK_FOLDER_TAG) + " " + pr.getString(Const.JDK_DST_TAG);
		
		String path = pr.getString(Const.JDK_DST_TAG).trim();
		path += path.charAt(path.length() - 1) == '/' ? "" : "/";
		String folder = pr.getString(Const.JDK_FOLDER_TAG).trim();
		folder += folder.charAt(folder.length() - 1) == '/' ? "" : "/";
		jdkpath = path + folder + "bin/java";
		logHandler.info("JDK path: " + jdkpath);
		String cmd3 = "sudo chmod +x " + path + folder + "bin/*";
		String cmd4 = "sudo /usr/sbin/update-alternatives --install /usr/bin/java java " 
				+ path + folder + "bin/java 300";
		String cmd5 = "sudo /usr/sbin/update-alternatives --install /usr/bin/javac javac "
				+ path + folder + "bin/javac 300";
		String cmd6 = "sudo /usr/sbin/update-alternatives --config java";
		
		dealcmd(cmd1 + "xxx" + cmd2 + "xxx" + cmd3 + "xxx" + cmd4 + "xxx" + cmd5 + "xxx" + cmd6);
//		dealcmd(cmd3 + "xxx" + cmd4 + "xxx" + cmd6 + "xxx" + cmd5);
		
	}
	
//	public static void main(String[] args){
//		String host = "deke@dlnode27";
//		String passwd = "111111";
//		LocateJDKCmd lc = new LocateJDKCmd(host, passwd);
//		lc.confjdk();
//	}

}
